package com.lfy.yunpicture.manager.websocket;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.lfy.yunpicture.manager.auth.SpaceUserAuthManager;
import com.lfy.yunpicture.manager.auth.model.SpaceUserPermissionConstant;
import com.lfy.yunpicture.model.entity.Picture;
import com.lfy.yunpicture.model.entity.Space;
import com.lfy.yunpicture.model.entity.User;
import com.lfy.yunpicture.model.enums.SpaceTypeEnum;
import com.lfy.yunpicture.service.PictureService;
import com.lfy.yunpicture.service.SpaceService;
import com.lfy.yunpicture.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class PictureEditInterceptor implements HandshakeInterceptor {
    @Resource
    private PictureService pictureService;
    @Resource
    private SpaceService spaceService;
    @Resource
    private UserService userService;
    @Resource
    private SpaceUserAuthManager spaceUserAuthManager;

    @Override
    public boolean beforeHandshake(@NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response, @NotNull WebSocketHandler wsHandler, @NotNull Map<String, Object> attributes) throws Exception {
        // 获取请求参数
        HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        String pictureId = servletRequest.getParameter("pictureId");
        // 进行参数校验
        if (StrUtil.isBlank(pictureId)) {
            return false;
        }
        Picture picture = pictureService.getById(pictureId);
        if (picture == null) {
            log.error("picture not exist");
            return false;
        }
        // 获取Picture对应的空间
        Long spaceId = picture.getSpaceId();
        if (spaceId == null) {
            return false;
        }
        Space space = spaceService.getById(spaceId);
        // 只有团队空间才有协同编辑功能
        if (space != null && space.getSpaceType().equals(SpaceTypeEnum.TEAM.getValue())) {
            User loginUser = userService.getLoginUser(servletRequest);
            // 获取当前用户的编辑权限
            List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
            if (CollUtil.isEmpty(permissionList)) {
                return false;
            }
            // 只有有编辑权限的才能进行编辑
            if (!permissionList.contains(SpaceUserPermissionConstant.PICTURE_EDIT)) {
                return false;
            }
            // 将参数放入 attributes 中，供后续使用
            attributes.put("pictureId", pictureId);
            attributes.put("user", loginUser);
        } else {
            return false;
        }
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}
